package algorithm.queue;

import algorithm.array.MyArray;

/**
 * @author bingo
 * @Description    数组队列
 * @Date 2018/6/2
 */
public class ArrayQueue<E> implements  Queue<E> {

    private MyArray<E> myArray;

    public ArrayQueue(int capacity) {
        this.myArray = new MyArray<>(capacity);
    }

    @Override
    public void enqueue(E e) {

        myArray.insertLast(e);
    }

    @Override
    public E dequeue() {

        return myArray.removeFirst();

    }

    @Override
    public E front() {
        return myArray.getFirst();
    }

    @Override
    public int getSize() {

       return myArray.getSize();
    }

    @Override
    public boolean isEmpty() {
        return myArray.isEmpty();
    }

    @Override
    public String toString(){

       StringBuilder result = new StringBuilder();
       result.append("Queue[");
      for(int i=0;i<myArray.getSize();i++){
        result.append(myArray.get(i));
        if(i<myArray.getSize()-1){
            result.append(',');
        }
      }
      result.append("]");
      return  result.toString();
    }

    public static void main(String[] args) {
        ArrayQueue arrayQueue = new ArrayQueue(10);
        arrayQueue.enqueue(1);
        arrayQueue.enqueue(2);
        arrayQueue.enqueue(3);
        System.out.println(arrayQueue.toString());


        arrayQueue.dequeue();
        System.out.println(arrayQueue.toString());

        arrayQueue.dequeue();
        System.out.println(arrayQueue.toString());
    }
}
